//
//  CreditServerHelper.m
//

#import "CreditServerHelper.h"
#import "LoginViewController.h"

@implementation CreditServerHelper

+ (void)logoutController{
    [[CurrentUser mine] reset];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    LoginViewController *login = [[LoginViewController alloc]init];
    UINavigationController *naV = [[UINavigationController alloc] initWithRootViewController:login];
    window.rootViewController = naV;
}

+ (void)tokenLoseToLogoutController:(BOOL)isExpire{
    [self logoutController];
    [[CreditCore globleCore] showAlert:isExpire];
}

+ (void)postCreditPhotoUrl:(NSString *)url params:(NSDictionary *)params imageData:(NSData *)imgData name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType withProgress:(ProgressBlock)progress success:(SuccessBlock)success failure:(FailureBlock)failure{
    
    NSString *fullPath = url;
    if([url hasPrefix:@"http"])
        fullPath =  url;
    else{
        fullPath = [NSString stringWithFormat:@"%@%@", kBaseUrl, url];
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 15;
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
    fullPath = [fullPath stringByAppendingFormat:@"?meal=%@&warmed=%@&boyfine=%@&inquiring=%@&kitchen=%@&duty=%@&cheerless=%@&blazed=%@",@"ios",@"mmla",@"common",kAppVersion,esString([[UIDevice currentDevice] name]),esString([UIDevice currentDevice].systemVersion),esString([[UIDevice currentDevice] identifierForVendor].UUIDString),esString([[UIDevice currentDevice] identifierForVendor].UUIDString)];
    if (!PMStringIsEmpty(esString([CurrentUser mine].sessionKey))) {
        fullPath = [fullPath stringByAppendingFormat:@"&brightly=%@",esString([CurrentUser mine].sessionKey)];
    }
    fullPath = [fullPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [manager POST:fullPath parameters:params headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imgData name:name fileName:fileName mimeType:mimeType];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
}


+ (void)uploadMaiDianDataStartTimeStr:(NSString *)startTime andEndTimeStr:(NSString *)endTime withTypeStr:(NSString *)typeStr withFirmly:(NSString *)firmly withSmaller:(NSString *)smaller{
    
    
    NSString *idfaStr = [[NSUserDefaults standardUserDefaults] valueForKey:kIDFAStr];
    NSString *longitude = [[NSUserDefaults standardUserDefaults] valueForKey:kLongitude];
    NSString *latitude =  [[NSUserDefaults standardUserDefaults] valueForKey:kLatitude];
    
    NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:@{
        @"quarrel":@"quarrelMianDian",
        @"claimed":typeStr,
        @"morpeth":esString([UIDevice currentDevice].identifierForVendor.UUIDString),
        @"smash":PMStringIsEmpty(idfaStr) ? @"":idfaStr,
        @"army":PMStringIsEmpty(latitude) ?  @"": latitude,
        @"sore":PMStringIsEmpty(longitude) ? @"": longitude,
        @"bandaged":startTime,
        @"riding":endTime,
        @"firmly":PMStringIsEmpty(firmly)?@"":firmly,
        @"smaller":PMStringIsEmpty(smaller)?@"":smaller,
    }];
    [BaseDataEngine apiForPath:kUploadMaiDianUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            return;
        }        
    } onCompleteFailur:^(NSError *error) {
        
    } withIsForm:YES];
}

@end
